// @system GO-CRAWL <shop-yun.com>
// @version go-crawl 1.0
// @author xiaotangren  <unphp@qq.com>
// @data 2014-07-29
// 初始化配置

package conf

import (
	"flag"
	base "shop-yun.go/go-crawl/utils/base"
	//"fmt"
	config "github.com/gokyle/goconfig"
	"os"
	"os/exec"
	"path/filepath"
	"strings"
)

var (
	CoreConf   *Conf
	CoreLog    *Log
	CoreDbpool *base.Dbpool
)

// 初始化配置参数
func init() {
	defaultConfFile := rootPath() + "/conf/conf.ini"
	configfile := flag.String("conf", defaultConfFile, "The conf of path")
	flag.Parse()
	initconf, err := config.ParseFile(*configfile)
	if err != nil {
		panic("conf.ini can't open")
	}

	CoreConf = &Conf{
		Initconf: initconf,
	}

	CoreLog = &Log{
		ChanLogMsg:    newMyLogChan(),
		ServerSafeMap: base.NewSafeMapRun(),
		AppSafeMap:    base.NewSafeMapRun(),
		Mode:          0,
		AppLogMap:     make(map[string]interface{}),
	}
	CoreLog.Init()

	CoreDbpool = &base.Dbpool{
		DbType: CoreConf.Get("db", "dbtype").String(),
		DbHost: CoreConf.Get("db", "dbhost").String(),
		DbName: CoreConf.Get("db", "dbname").String(),
		DbUser: CoreConf.Get("db", "dbusername").String(),
		DbPawd: CoreConf.Get("db", "dbpassword").String(),
		Log:    CoreLog,
	}
}

func rootPath() string {
	root, _ := exec.LookPath(os.Args[0])
	rootPath, _ := filepath.Abs(root)
	tempSlice := strings.Split(rootPath, "/")
	return strings.Join(tempSlice[0:len(tempSlice)-1], "/")
}
